package com.atmilan.quartz.utils.product;

import com.alibaba.fastjson.JSONObject;
import com.atmilan.quartz.config.RestTemplateConfig;
import com.atmilan.quartz.model.dto.xyy.XyyProductResDTO;
import com.atmilan.quartz.model.dto.xyy.XyyProductSkuResDTO;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 拉取商品源数据
 */

@Slf4j
@Service
public class XyyProductUtil {

    @Autowired
    private RestTemplateConfig restTemplateConfig;

    @Value("${xyy.appKey}")
    private String xyyAppKey;
    @Value("${xyy.appSecret}")
    private String xyyAppSecret;
    @Value("${xyy.url}")
    private String xyyUrl;

    /**
     * 查询分页商品数据
     * @Description:
     * @author daigb
     * @param @param pageNow
     * @param @param pageSize
     * @param @return
     * @param @throws Exception
     * @return InProductResDTO
     * @throws
     */
    public XyyProductResDTO selectXyyPage(Integer pageNow, Integer pageSize, Long time, String token) throws Exception {
        // 组装请求url
        XyyProductResDTO xyyProductResDTO = null;
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(xyyUrl);
            sb.append("/Index/t/");
            sb.append(time);
            sb.append("/r/");
            sb.append(time);
            sb.append("/appkey/");
            sb.append(xyyAppKey);
            sb.append("/secret/");
            sb.append(xyyAppSecret);
            sb.append("/s/");
            sb.append(token);
            sb.append("/p/");
            sb.append(pageNow);
            sb.append("/n/");
            sb.append(pageSize);
            // 发送请求
            RestTemplate restTemplate = restTemplateConfig.restTemplate();
            ResponseEntity<String> response = restTemplate.getForEntity(sb.toString(), String.class);

            if (HttpStatus.OK != response.getStatusCode()) {
                JSONObject message = JSONObject.parseObject(response.getBody());
                String errorMessage = message.getString("error");

                log.info("--------------- xyy查询分页商品数据异常{} ---------------", errorMessage);
            }

            ObjectMapper mapper = new ObjectMapper();
            xyyProductResDTO = mapper.readValue(response.getBody(), XyyProductResDTO.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return xyyProductResDTO;
    }

    /**
     * 获取商品sku
     * @Description:
     * @author daigb
     * @param @param ids
     * @param @return
     * @param @throws Exception
     * @return InProductResDTO
     * @throws
     */
    public List<XyyProductSkuResDTO> getXyySkus(Integer goodsId, Long time, String token) throws Exception {
        // 组装请求url
        List<XyyProductSkuResDTO> xyyProductSkuResDTOs = null;
        try {
            StringBuilder sb = new StringBuilder();
            sb.append(xyyUrl);
            sb.append("/getGoodsStock/t/");
            sb.append(time);
            sb.append("/r/");
            sb.append(time);
            sb.append("/appkey/");
            sb.append(xyyAppKey);
            sb.append("/secret/");
            sb.append(xyyAppSecret);
            sb.append("/s/");
            sb.append(token);
            sb.append("/gid/");
            sb.append(goodsId);
            // 发送请求
            RestTemplate restTemplate = restTemplateConfig.restTemplate();
            ResponseEntity<String> response = restTemplate.getForEntity(sb.toString(), String.class);
            if (HttpStatus.OK != response.getStatusCode()) {
                JSONObject message = JSONObject.parseObject(response.getBody());
                String errorMessage = message.getString("error");

                log.info("--------------- xyy获取商品sku异常{} ---------------", errorMessage);
            }

            ObjectMapper mapper = new ObjectMapper();
            JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, XyyProductSkuResDTO.class);
            xyyProductSkuResDTOs = mapper.readValue(response.getBody(), javaType);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return xyyProductSkuResDTOs;
    }

    public String getXyyToken(long time) {
        // 生成秘钥
        String sign = null;
        try {
            sign = DigestUtils.sha1Hex(time + "" + time + xyyAppKey + xyyAppSecret);
//        String sign = DigestUtils.sha1Hex(time + "" + time + "xunwu" + "xunwucec51ef9aa7663d9675b0104749495skocFg");
            sign = DigestUtils.md5Hex(sign);
            sign = DigestUtils.md5Hex(sign);
            sign = sign.toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sign;
    }
}
